﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FListVideo : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FListVideo";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("FTitle", "Danh sách video");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Ngầm định (Chuẩn)"),1)
                };
            }
        }

      
        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FListVideo";
            }
        }        

        [AjaxPro.AjaxMethod]
        public string GetHtml(string VideoId, int ItemsPerRow, int NoOfVisiblePage, int CurrentPage, string JumpJavaScript)
        {
            string RetHtml = "";
            DataTable dtItems = new VideoBLL().ReadingItems(VideoId);
            Portal.Environment.PagingCls
                    paging = new Portal.Environment.PagingCls();
            int StartIndex = 0;
            int EndIndex = 0;

            string pagingText = new PagingCls().GetPaging(VideoId, dtItems.Rows.Count, 10, 10, CurrentPage, "JumpPageVideoDetail", ref StartIndex, ref EndIndex);

            RetHtml +=
                  "<div class=\"PageTitleBar\">\n" +
                        "				<ul class=\"breadcrumb\">\n" +
                        "					<li class=\"breadcrumblv1\"><a href=\"#\">Danh sách video</a></li>\n" +
                        "				</ul>\n" +
                        "				<div class=\"clr\"></div>\n" +
                       "</div>" +
                        "<div class=\"ArticleDetail\">\n" +
                        "<ul class=\"listnews\">" ;                
            for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
            {
                string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                string AdvImageId = (string)dtItems.Rows[iIndex]["fk_AdvImage_Id"];
                string belongSite = (string)dtItems.Rows[iIndex]["belongSite"];
                string Subject = "";
                try
                {
                    Subject = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "AdvImageItemName"];
                }
                catch { }
                string Description = "";
                try
                {
                    Description = (string)dtItems.Rows[iIndex]["Description"];
                }
                catch { }
                string EntryDate = ((DateTime)dtItems.Rows[iIndex]["ItemEntryDate"]).ToString();
                string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                string Url = GetVideoDetails(belongSite, Subject, AdvImageItemId.Substring(0, 8));                            
                string VideoUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;             

                RetHtml += "<li>" +
                             "<a href=\"" + Url + "\" title=\"" + Subject + "\"><img src=\"images/video.jpg\" alt=\"" + Subject + "\" />" +
                             "<span>" + Subject + "</span></a>" +
                             "<span class=\"greynews\">" + DateTime.Parse(EntryDate).ToString("dd/MM/yyyy") + "</span>" +
                             "<p>" + Description + "</p>" +
                             "<div class=colfix>" +
                             "</li>";
            }
            RetHtml += "</ul></div>";
            RetHtml += "<div class=\"pagenavi\"><ul>" + pagingText + "</ul><br class=\"colfix\" /></div>";
            return RetHtml;
        }

        public string GetVideoDetails(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.FrontHttpRoot + "/" + belongSite + "/" + Subject + "-video-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            //string Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-video-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;

            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }
            
        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string VideoId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            if (VideoId == null) VideoId = "";
            if (VideoId.Trim().Length == 0)
            {
                return "<div class=\"clrfix\"></div>" +
                       "<div class=cssAccessDenided>" + GetText("AccessDenided", "Yêu cầu truy cập không hợp lệ!") + "</div>";
            }
            DataTable dtItems = new VideoBLL().ReadingItems(VideoId);
            string RetHtml = "";
            if (dtItems.Rows.Count == 0)
            {
                return "<div class=\"clrfix\"></div>"+
                       "<div class=cssNoImageForVideo>" + GetText("NoImage", "Không có ảnh nào trong bộ sưu tập ")+"</div>";
            }

            RetHtml +=
                 Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CDSP", "video.js") +
                "<div id=\"leftsub\">" +
                 GetHtml(VideoId, 10, 10, 0, "JumpPageVideoDetail") +
                "</div>";
            return RetHtml;
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("DefaultView"))
                {
                    return ClientRenderWebPart(TabModuleId, HasPorlet);
                }
                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

       

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            string Query = "select Id=AdvImageId,Subject=[" + ActiveLanguage + "Subject] from TableAdvImage where belongSite='" + belongSite + "'";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
